package com.chun.controller;

import com.chun.pojo.*;
import com.chun.service.EmpService;
import com.chun.utils.AliOSSUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.time.LocalDate;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/emps")
public class EmpController {
    @Autowired
    private EmpService empService;

    @Autowired
    AliOSSUtils aliOSSUtils;

    /*
    分页查询
     */
//    @GetMapping
//    public Result findAll(@RequestParam(defaultValue = "1") Integer pageNo, @RequestParam(defaultValue = "5") Integer pageSize) {
//        PageBean pageBean = empService.list(pageNo, pageSize);
//        log.info("分页查询:{},{}", pageBean, pageSize);
//        return Result.success(pageBean);
//    }
    @GetMapping
    public Result find(String name, Short gender, Short job, @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
                       @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end,
                       @RequestParam(defaultValue = "1") Integer pageNo,
                       @RequestParam(defaultValue = "10") Integer pageSize) {
        PageBean pageBean = empService.list2(name, gender, job, begin, end, pageNo, pageSize);
        log.info("按条件分页查询:{},{}", pageBean, pageNo);
        return Result.success(pageBean);
    }

    @DeleteMapping("/{ids}")
    public Result delete(@PathVariable Integer[] ids) {
        empService.deleteByIds(ids);
        log.info("根据id删除员工");
        return Result.success();
    }

    @PostMapping
    public Result add(@RequestBody Emp emp) {
        empService.add(emp);
        log.info("添加员工");
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result findById(@PathVariable Integer id) {
        Emp emp = empService.findById(id);
        log.info("通过id:{}查询员工", emp);
        return Result.success(emp);
    }
    @GetMapping("/report/empGenderData")
    public Result getEmpGenderData() {
        List<GenderCountDTO> genderCountDTOList = empService.getEmpGenderData();
        return Result.success(genderCountDTOList);
    }

    @GetMapping("/report/empJobData")
    public Result getEmpJobData() {
        JobDataCountDTO jobDataCountDTO = empService.getEmpJobData();
        return Result.success(jobDataCountDTO);
    }

    @PutMapping
    public Result update(@RequestBody Emp emp) {
        empService.update(emp);
        log.info("修改员工信息");
        return Result.success();
    }


    @PostMapping("/upload")
    public Result upload(MultipartFile image) throws IOException {
//        String originalFilename = image.getOriginalFilename();
//        String fileName = UUID.randomUUID().toString()+originalFilename.substring(originalFilename.lastIndexOf("."));
//        File f = new File("E:/images/" + fileName);
//        image.transferTo(f);
//        log.info("上传文件");
//        return Result.success();
        String url = "https://" + aliOSSUtils.upload(image);
        log.info("上传文件url:{}", url);
        return Result.success(url);
    }
}
